<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h5 class="modal-title" th:text="${'修改阈值>' + threshold.thresholdName}"></h5>
</div>

<form id="defaultForm" method="post" class="form-horizontal" action="/alarm/threshold_editManage">
    <input name="id" style="display: none" th:value="${threshold.id}">
    <div class="modal-body">
        <div class="form-group">
            <label class="col-lg-3 control-label">当前值</label>
            <div class="col-lg-5" th:text="${threshold.setVal}">
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-3 control-label">修改值</label>
            <div class="col-lg-5">
                <input type="text" class="form-control" name="setVal"/>
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-3 control-label">修改口令</label>
            <div class="col-lg-5">
                <input type="text" class="form-control" onchange="changePassWord()" name="changePassword" />
            </div>
            <sanp id = "accountText"></sanp>
        </div>
    </div>

    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="submit"  id="saveBtn" class="btn btn-primary" disabled="disabled">保存</button>
    </div>
</form>
<script>
    $('#defaultForm').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        }, fields: {
            code: {
                validators: {
                    notEmpty: {
                        message: 'CODE不能为空'
                    }
                }
            },
            name: {
                validators: {
                    notEmpty: {
                        message: '名称不能为空'
                    }
                }
            }
        }
    }).on('success.form.bv', function(e) {
        // 阻止默认提交事件
        e.preventDefault();

        // 获取form表单
        var $form = $(e.target);

        // Get the BootstrapValidator instance
        var bv = $form.data('bootstrapValidator');
        console.log($('#defaultForm').serialize());

        $.ajax({
            url: $form.attr('action'),
            type: "post",
            data: $form.serialize(),
            dataType: 'json',
            success: function (result) {
                if (result.type == "alert") {
                    alert(result.message);
                }
                $("#modal").modal("hide");
                //刷新跳转至第一页
                // $("#dataTable").bootstrapTable("selectPage",1).bootstrapTable("refresh");
                $("#dataTable").bootstrapTable("selectPage", 1);
            },
            error: function () {
                console.log("error")
            }
        });
    });

    window.onload = function () {
        document.getElementById("defaultForm");
    }

    //校验
    function changePassWord() {
      var pwd =   $("input[name = 'changePassword']").val();
        var accountText = $("#accountText")[0];
        if(pwd != "") {
            $.ajax({
                url: "change_password",
                data: {"changePassword": pwd},
                type: "post",
                success: function (data) {
                    if (data == "口令错误,请重新输入") {
                        // 口令不正确
                        accountText.innerHTML = "<font color='red' size='3'>" + data + "</font>"
                        $("#saveBtn").attr("disabled", "disabled");
                    } else {
                        //口令正确
                        accountText.innerHTML = "<font color='red' size='3'>" + data + "</font>"
                        $("#saveBtn").removeAttr("disabled");
                    }
                }
            });
        }else {
            accountText.innerText = "";
        }
    }
</script>